<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\common\BaseController;
use App\Http\Controllers\Controller;
use App\Models\City;
use App\Models\House;
use App\Models\HouseAttr;
use App\Models\HouseImgModel;
use App\Services\AliyunUpload;
use App\Services\Curl;
use Illuminate\Http\Request;

class HouseCreate extends Controller
{
    /**
     * @param Request $request
     * @return false|string
     * 房源小区查询
     */
    public static function get_area(Request $request){
        $area_name = $request->get('area_name');
        $keyword = '住宅区';
        $request_url = sprintf(config('tencent_map.suggestion_url'),$area_name,$keyword,config('tencent_map.key'));
        $housesData = Curl::getRequest($request_url);
        $data = [];
        foreach ($housesData['data'] as $k => $v)
        {
            $arr = [
                'id' => $k,
                'houses_name' => $v['title'],
                'class' => 'chunk'
            ];
            array_push($data,$arr);
        }
        if (empty($data)){
            return BaseController::retJson(2000,'查询失败',true);
        }else{
            return BaseController::retJson(4001,'成功',$data);
        }
    }
    /**
     * 房源图片上传
     */
    public static function upload_file(Request $request){
        $path=$request->file('file')->getPathname();
        $type=$request->file('file')->getClientOriginalExtension();
        $url= AliyunUpload::upFile($path,$type);
        if ($url){
            return BaseController::retJson(2000,'上传成功',$url);
        }else{
            return BaseController::retJson(4001,'上传失败',false);
        }
    }
    /**
     * 获取属性值
     */
    public function getHouseData(Request $request)
    {
        //获取房源所在区
        $house_region=City::selArea('闵行区');
        //获取房产类型
        $propertyType = HouseAttr::getHouseattrData('house_property_type');
        //获取几室几厅几卫
        $houseRoom = Houseattr::getHouseattrData('house_room');
        //获取房源朝向
        $houseDirection = Houseattr::getHouseattrData('house_direction');
        //获取房源楼层
        $houseFloor = Houseattr::getHouseattrData('house_floor');
        //获取标签
        $houseLabel = Houseattr::getHouseattrData('house_label');
        //房源装修、配置
        $houseConfig= Houseattr::getHouseattrData('house_config');

        $houseAttr = [
            'house_region'=>$house_region,
            'house_property_type' =>$propertyType,
            'house_room' => $houseRoom,
            'house_direction' => $houseDirection,
            'house_floor' => $houseFloor,
            'house_label' => $houseLabel,
            'house_config'=>$houseConfig
        ];
        return BaseController::retJson(2000,'成功',$houseAttr);
    }
    /**
     * @param Request $request
     * 二手房出售
     */
    public static function create(Request $request){
         $uploads=$request->all()['upload'];
         $data=$request->except('upload');
         $data['house_type']=1;
         $data['house_province']=2;
         $data['house_city']=54;
         $data['house_region']=1216;
         //try & catch 捕获异常
        try {
            //添加数据
            $houseInfo=House::create($data);
            $id=$houseInfo->id;
            $uploads['house_id']=$id;
            HouseImgModel::create($uploads);
            //返回数据
            return BaseController::retJson(2000,'添加成功',true);
        }catch(\Exception $e){
            return BaseController::retJson(4000,$e->getMessage(),false);
        }
    }
    /**
     *我要买房
     */
    public static function house_purchase(Request $request){
        $data=$request->all();
        $data['house_type']=3;
        try {
            //添加买房数据
            $houseInfo=House::create($data);
            return BaseController::retJson(2000,'添加成功',true);
        }catch(\Exception $e){
            return BaseController::retJson(4000,$e->getMessage(),false);
        }
    }
}
